import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_architecture/constant/route_constants.dart';
import 'package:flutter_architecture/routes/routes.dart';
import 'package:flutter_architecture/utils/screen_util.dart';

/// 欢迎页面，倒计时widget
/// todo ui 待美化
/// @author: dingwen
/// @date: 2021/1/3

class TimeCounterReverseWidget extends StatefulWidget {
  @override
  _TimeCounterReverseWidgetState createState() => _TimeCounterReverseWidgetState();
}

class _TimeCounterReverseWidgetState extends State<TimeCounterReverseWidget> {
  //倒计时开始
  int _time = 5;

  //倒计时
  Timer _timer;

  @override
  void initState() {
    super.initState();
    //创建倒计时间隔1秒执行
    _timer = Timer.periodic(Duration(seconds: 1), (timer) {
      if (_time == 0) {
        _timer.cancel();
        //goHome
        goHome();
        return;
      }
      _time--;
      setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: sWidth(100),
      height: sHeight(33),
      alignment: Alignment.center,
      child: Text(
        '$_time s',
        style: TextStyle(
            fontSize: sFontSize(16),
            color: Colors.redAccent,
            fontWeight: FontWeight.w500),
      ),
      decoration: BoxDecoration(
          border: Border.all(color: Colors.redAccent, width: sWidth(2)),
          borderRadius: BorderRadius.all(Radius.circular(sWidth(20)))),
    );
  }

  ///去首页
  void goHome(){
//    Routes.push(context, settings: RouteSettings(name: home_page));
    Routes.pageTo(context, navigation_drawer__page);
  }

  @override
  void dispose() {
    super.dispose();
    if (_timer.isActive) {
      _timer.cancel();
    }
  }
}
